package com.zyd.androidwifi;

import static android.content.pm.PackageManager.PERMISSION_GRANTED;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class WiFiPanelActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_panel_wifi);

        ActivityResultLauncher<Intent> intentActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                Log.d("TAG", "onActivityResult: " + result.getData());
                //此处是跳转的result回调方法
//                if (result.getData() != null && result.getResultCode() == Activity.RESULT_OK) {
//                    result.getData().getStringExtra("AAA");
//                } else {
//                    Toast.makeText(getApplicationContext(), "empty_not_saved", Toast.LENGTH_LONG).show();
//                }
            }
        });

        Button buttonA = findViewById(R.id.connectWiFiA);
        Button buttonB = findViewById(R.id.connectWiFiB);
        buttonA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Intent panelIntent = new Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY);
//                startActivityForResult(panelIntent, 100);
                intentActivityResultLauncher.launch(panelIntent);

            }
        });

        buttonB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(WiFiPanelActivity.this);
                builder.setIcon(R.drawable.thumb_bg);
                builder.setTitle("提示");
                builder.setMessage("是否连接WIFI");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface pDialogInterface, int pI) {
                        Toast.makeText(WiFiPanelActivity.this, "确定", Toast.LENGTH_SHORT).show();

                        //跳到手机WIFI设置界面
                        startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));
                    }
                });
//                builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface pDialogInterface, int pI) {
//                        Toast.makeText(WiFiPanelActivity.this, "忽略", Toast.LENGTH_SHORT).show();
//                    }
//                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface pDialogInterface, int pI) {
                        Toast.makeText(WiFiPanelActivity.this, "取消", Toast.LENGTH_SHORT).show();
                    }
                });
                builder.create().show();

                //跳到手机WIFI设置界面
//                startActivity(new Intent( android.provider.Settings.ACTION_WIFI_SETTINGS));
//                startActivity(new Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY));
            }
        });
    }

}